import router from './index'
import { useUserStore } from '@/store/modules/user'

const allowList = ['login'] // 白名单
const loginRoutePath = '/login'
const defaultRoutePath = '/'

router.beforeEach((to, from, next) => {
  // ✅ 这将起作用，因为路由器在之后开始导航
  // 路由已安装，pinia 也将安装
  const userStore = useUserStore()
  const token = userStore.token

  console.log('%c [ token ]-15', 'font-size:13px; background:pink; color:#bf2c9f;', token)

  if (token) {
    if (to.path === loginRoutePath) {
      next({ path: defaultRoutePath })
    } else {
      next()
    }
  } else {
    if (allowList.includes(to.name)) {
      // 在免登录名单，直接进入
      next()
    } else {
      next({ path: loginRoutePath, query: { redirect: to.fullPath } })
    }
  }
})
